package com.example.demo.controller;

import com.example.demo.model.Userinfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    @Transactional
    public int add() {
        //1.非空判断
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername("张三");
        userinfo.setPassword("123");
        //2.调用 service 执行添加
        int result = userService.add(userinfo);
        System.out.println("result: "+result);
        try {
            int num = 10/0;
        }catch(Exception e) {
//            throw e;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        //3.将结果给前端
        return 0;
    }
}
